/**
 * 项目名称：MSH
 * 包名称：com.mt.common.sys.serviceImpl
 * 文件名称：PermissionServiceImpl.java
 */
package com.mt.system.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;


import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;

import com.mt.component.exception.DataOperateException;
import com.mt.system.dao.PermissionDao;
import com.mt.system.domain.Permission;
import com.mt.system.domain.Role;
import com.mt.system.domain.User;
import com.mt.system.service.PermissionService;


/**
 * 类名称：PermissionServiceImpl <br>
 * 类描述： 权限service<br>
 * 创建人： 马腾 <br>
 * 创建时间：Nov 24, 2012 10:16:43 PM <br>
 * 修改人： 马腾 <br>
 * 修改时间：Nov 24, 2012 10:16:43 PM <br>
 */
@Service("PermissionService")
@RemotingDestination(channels = {"my-amf"})
public class PermissionServiceImpl implements PermissionService {
	
	@Resource
	private PermissionDao permissionDao;

	@Override
	public Permission addPermission(Permission permission) {
		return permissionDao.addPermission(permission);
	}

	@Override
	public List<Permission> getPermissions() {
		return permissionDao.getPermissions();
	}

	@RemotingInclude
	@Override
	public List<Permission> getPermissionTreeByUser(User user) {
		List<Permission> allPermissions = permissionDao.getPermissionsByUser(user);
		List<Permission> permissions = new ArrayList<Permission>();
		for (Permission permission : allPermissions) {
			if(permission.getParent() == null){
				permissions.add(permission);
				permission.setChildren(findChidlren(permission, allPermissions));
			}
		}
		return permissions;
	}
	
	private List<Permission> findChidlren(Permission parent , List<Permission> allPermissions){
		List<Permission> children = new ArrayList<Permission>();
		for (Permission permission : allPermissions) {
			if(parent == permission.getParent() || parent.equals(permission.getParent())){
				children.add(permission);
				permission.setChildren(findChidlren(permission, allPermissions));
			}
		}
		if(children.isEmpty()) return null;
		return children;
	}
	
	@Override
	public List<Permission> getPermissionTreeByRole(Role role) {
		List<Permission> allPermissions = permissionDao.getPermissionsByRole(role);
		List<Permission> permissions = new ArrayList<Permission>();
		for (Permission permission : allPermissions) {
			if(permission.getParent() == null){
				permissions.add(permission);
				permission.setChildren(findChidlren(permission, allPermissions));
			}
		}
		return permissions;
	}

	@RemotingInclude
	@Override
	public List<Permission> getPermissionTreeByUser(User user, Role role) {
		if(role == null){
			return getPermissionTreeByUser(user);
		}
		List<Permission> rolePermissions = permissionDao.getPermissionsByRole(role);
		List<Permission> allPermissions = permissionDao.getPermissionsByUser(user);
		for (Permission permission : rolePermissions) {
			if(!allPermissions.contains(permission)){
				throw new DataOperateException(DataOperateException.OPERATE_FAILURE , "当前用户的权限小于该角色的权限，不能对其进行修改！");
			}
		}
		for (Permission permission : allPermissions) {
			permission.setSelected(rolePermissions.contains(permission));
		}
		List<Permission> permissions = new ArrayList<Permission>();
		for (Permission permission : allPermissions) {
			if(permission.getParent() == null){
				permissions.add(permission);
				permission.setChildren(findChidlren(permission, allPermissions));
			}
		}
		return permissions;
	}

	@RemotingInclude
	@Override
	public List<Permission> getPermissionByRole(Role role) {
		return permissionDao.getPermissionsByRole(role);
	}
}
